package com.rockxin.cat.activity;

import android.content.Intent;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.rockxin.cat.MainActivity;
import com.rockxin.cat.R;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.base.Constant;
import com.rockxin.cat.base.User;
import com.rockxin.cat.bean.UseBean;
import com.rockxin.cat.bean.UserBean;
import com.rockxin.cat.event.LoginEvent;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.SPUtils;
import com.rockxin.cat.utils.ToastUtils;

import org.simple.eventbus.EventBus;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import rx.Subscriber;

/**
 * Created by cat on 18-3-2.
 */

public class LoginActivity extends BaseActivity {
    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_passwd)
    EditText etPasswd;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.tv_forgot_password)
    TextView tvForgotPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @Override
    protected int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initData() {
        initView();
        setListener();
    }

    public void initView() {
        title.setText("登录");
        etPhone.setSingleLine();
        etPasswd.setSingleLine();
        etPasswd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

    }

    private void setListener() {
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
        tvForgotPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, ResetLoginPasswdActivity.class);
                startActivity(intent);
            }
        });
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });
    }

    private boolean isMobileNO() {
        final String phone = etPhone.getText().toString();
        Pattern p = Pattern.compile("^((13[0-9])|(14[0-9])|(15[^4,\\D])|(17[0-9])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(phone);
        boolean bool = !m.matches();
        return bool;
    }

    public void login() {
        final String phone = etPhone.getText().toString();
        final String passwd = etPasswd.getText().toString();
        if (isMobileNO()) {
            ToastUtils.showShortToastSafe("手机号不正确");
            return;
        } else if (passwd.length() < 6) {
            ToastUtils.showShortToastSafe("密码不能小于6位");
            return;
        }

        final CatNetWork catNetWork = new CatNetWork();
        catNetWork.login(phone, passwd, new Subscriber<HttpResult<UseBean>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpResult<UseBean> useBeanHttpResult) {
                Logger.json(new Gson().toJson(useBeanHttpResult));
                Logger.d(useBeanHttpResult.getData().getToken());
                if (useBeanHttpResult.getStatus() == 1) {
                    SPUtils spUtils = new SPUtils("Guide");
                    spUtils.put(Constant.PREFERENCES.PHONE, phone);
                    spUtils.put(Constant.PREFERENCES.PASSWORD, passwd);
                    spUtils.put(Constant.PREFERENCES.TOKEN, useBeanHttpResult.getData().getToken());

                    EventBus.getDefault().post(new LoginEvent(useBeanHttpResult.getData().getToken()), "login");
                    final UserBean userInfo = new UserBean();
                    userInfo.setToken(useBeanHttpResult.getData().getToken());
                    User.getInstance().saveUserInfo(userInfo);
                    finish();
                } else {
                    ToastUtils.showShortToastSafe(useBeanHttpResult.getInfo());
                }
            }
        });
    }
}
